package cn.itsource.quartz.service.impl;

import cn.itsource.quartz.domain.QuartzJobInfo;
import cn.itsource.quartz.job.MainJob;
import cn.itsource.quartz.service.IQuertzService;
import cn.itsource.quartz.utils.QuartzUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuertzServiceImpl implements IQuertzService {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Override
    public void addJob(QuartzJobInfo info) {
        /*
           Scheduler sched:调度器
           String jobName：任务的名字【唯一性】
           Class cls：具体任务要完成的工作
           Object params：定时任务要携带参数  【参数在任务详情里面使用】
           String time：触发的时间
         */
        QuartzUtils.addJob(schedulerFactoryBean.getScheduler(),info.getJobName() ,
                MainJob.class ,info.getParams() ,info.getCronj() );
    }

    @Override
    public void removeJob(String jobName) {
        /*
           Scheduler sched:调度器
           String jobName：任务的名字【唯一性】
         */
        QuartzUtils.removeJob(schedulerFactoryBean.getScheduler(),jobName );
    }
}
